/** 全局存储 */

import { useStorageSync } from '@uni-helper/uni-use'

/**
 * 数据存储
 *
 * @param name 键
 * @param value 值
 */
function set<T>(name: string, value: T) {
  useStorageSync(name, '').value = JSON.stringify(value)
}

/**
 * 数据读取
 *
 * @param name 键
 * @returns The parsed value of the stored data, or undefined if not found.
 */
function get<T>(name: string): T | undefined {
  const value = useStorageSync(name, undefined).value
  if (value) {
    return JSON.parse(value) as T
  }
  return undefined
}

/**
 * 数据删除
 *
 * @param name 键
 */
function remove(name: string) {
  useStorageSync(name, '').value = undefined
}

export default { set, get, remove }
